//
//  TKYGroupController.m
//  StationHouseHelper
//
//  Created by apple on 2018/6/21.
//  Copyright © 2018年 铁科院. All rights reserved.
//

#import "TKYGroupController.h"
#import "TKYMineCell.h"
#import "TKYMineDetaileController.h"
#define kCell_Height 60
@interface TKYGroupController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)NSString *fileNameStr;

@property (nonatomic, strong)NSMutableArray  *sectionArray;
@property (nonatomic, strong)NSMutableArray  *stateArray;
@property (nonatomic, strong)NSMutableArray *selectDataArr;
@end

@implementation TKYGroupController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initDataSource];
    [self.view addSubview:self.tableV];
    self.selectDataArr = [NSMutableArray array];
    // Do any additional setup after loading the view.
}
#pragma mark-- 初始化数据
- (void)initDataSource
{
    self.sectionArray  = [NSMutableArray arrayWithObjects:@"文件1",
                          @"文件2",
                          @"文件3",
                          @"文件4",nil];
    
    NSArray *one = @[@"文件1",@"文件1",@"文件1"];
    NSArray *two = @[@"文件2",@"文件2？",@"文件2"];
    NSArray *three = @[@"文件3",@"文件3",@"文件3"];
    NSArray *four = @[@"文件4文件4文件4文件4文件4文件4",@"文件4",@"文件4",@"文件4"];
    
    self.dataSource = [NSMutableArray arrayWithObjects:one,two,three,four, nil];
    self.stateArray = [NSMutableArray array];
    
    for (int i = 0; i < self.dataSource.count; i++)
    {
        //所有的分区都是闭合
        [self.stateArray addObject:@"0"];
    }
}

#pragma mark --- UITableViewDataSource,UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([self.stateArray[section] isEqualToString:@"1"]){
        //如果是展开状态
        NSArray *array = [self.dataSource objectAtIndex:section];
        return array.count;
    }else{
        //如果是闭合，返回0
        return 0;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TKYMineCell *cell = [tableView dequeueReusableCellWithIdentifier:kMineCellID];
    cell.selectedBtn.hidden = YES;
    cell.nameLabel.textAlignment = NSTextAlignmentLeft;
    cell.nameLabel.text = self.dataSource[indexPath.section][indexPath.row];
    cell.backgroundColor = [UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleDefault;
                                        
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return self.sectionArray[section];
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
    [button setTag:section+1];
    button.backgroundColor = [UIColor whiteColor];
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [button setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 60)];
    [button addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
    UIImageView *line = [[UIImageView alloc]initWithFrame:CGRectMake(0, button.frame.size.height-1, button.frame.size.width, 1)];
    [line setImage:[UIImage imageNamed:@"line_real"]];
    [button addSubview:line];
    
    UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(10, (kCell_Height-22)/2, 22, 22)];
    [imgView setImage:[UIImage imageNamed:@"ico_faq_d"]];
    [button addSubview:imgView];
    
    UIImageView *_imgView = [[UIImageView alloc]initWithFrame:CGRectMake(20, (kCell_Height-6)/2, 10, 6)];
    
    if ([self.stateArray[section] isEqualToString:@"0"]) {
        _imgView.image = [UIImage imageNamed:@"ico_listdown"];
    }else if ([self.stateArray[section] isEqualToString:@"1"]) {
        _imgView.image = [UIImage imageNamed:@"ico_listup"];
    }
    [button addSubview:_imgView];
    
    UILabel *tlabel = [[UILabel alloc]initWithFrame:CGRectMake(45, (kCell_Height-20)/2, 200, 20)];
    [tlabel setBackgroundColor:[UIColor clearColor]];
    [tlabel setFont:[UIFont systemFontOfSize:14]];
    [tlabel setText:self.sectionArray[section]];
    [button addSubview:tlabel];
    return button;
}

#pragma mark  -select cell

/**
 页眉按钮点击
 */
- (void)buttonPress:(UIButton *)sender//headButton点击
{
    //判断状态值
    if ([self.stateArray[sender.tag - 1] isEqualToString:@"1"]){
        //修改
        [self.stateArray replaceObjectAtIndex:sender.tag - 1 withObject:@"0"];
    }else{
        [self.stateArray replaceObjectAtIndex:sender.tag - 1 withObject:@"1"];
    }
    [self.tableV reloadSections:[NSIndexSet indexSetWithIndex:sender.tag-1] withRowAnimation:UITableViewRowAnimationAutomatic];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kCell_Height;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.00001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return kCell_Height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    TKYMineDetaileController *mineDetaileVC = [[TKYMineDetaileController alloc]init];
    
    [self.navigationController pushViewController:mineDetaileVC animated:YES];
}

#pragma mark -- 懒加载
- (BaseTabelView *)tableV{
    if (_tableV == nil) {
        _tableV = [[BaseTabelView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)style:UITableViewStyleGrouped];
        _tableV.delegate = self;
        _tableV.dataSource = self;
        [_tableV registerNib:[UINib nibWithNibName:@"TKYMineCell" bundle:nil] forCellReuseIdentifier:kMineCellID];
    }
    return _tableV;
}


@end
